﻿namespace EyeOpen.SilverlightConverter.Windows
{
    using System;
    using System.IO;
    using System.Text.RegularExpressions;
    using System.Windows.Forms;
    using System.Xml.Linq;

    internal partial class MainForm : Form, IErrorSubscriber
    {
        private Exception lastError;

        private VisualStudioProject visualStudioProject;

        private FileInfo projectFileInfo;

        public MainForm()
        {
            this.InitializeComponent();
        }

        public void RegisterError(Exception lastError)
        {
            this.lastError = lastError;
            this.errorOccuredToolStripStatusLabel.Visible = true;
        }

        private void LoadVisualStudioProjectLinkLabel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "C# Project files (.csproj)|*.csproj|VB Project files (.vbproj)|*.vbproj";
            openFileDialog.Title = "Open Visual Studio project file";
            openFileDialog.ShowDialog(this);
            openFileDialog.Multiselect = false;
            openFileDialog.SupportMultiDottedExtensions = true;
            openFileDialog.CheckFileExists = true;
            openFileDialog.CheckPathExists = true;

            if (File.Exists(openFileDialog.FileName))
            {
                this.projectFileNameTextBox.Text = openFileDialog.FileName;
            }
        }

        private void AnalyzeLibraryLinkLabel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            this.convertedLabel.Visible = false;
            
            if (string.IsNullOrEmpty(this.projectFileNameTextBox.Text))
            {
                this.mainErrorProvider.SetError(this.loadVisualStudioProjectLinkLabel, "Please provide a project file name.");
                return;
            }

            this.projectFileInfo = new FileInfo(this.projectFileNameTextBox.Text);

            if (!this.projectFileInfo.Exists)
            {
                this.mainErrorProvider.SetError(this.loadVisualStudioProjectLinkLabel, "The specified project file must exist.");
                return;
            }

            string projectContent = File.ReadAllText(this.projectFileNameTextBox.Text);

            this.visualStudioProject = new VisualStudioProject(projectContent);

            this.isSilverlightLibraryCheckBox.Checked = this.visualStudioProject.IsSilvelightLibrary;
            this.isWindowsLibraryCheckBox.Checked = !this.isSilverlightLibraryCheckBox.Checked;

            this.startConversionLinkLabel.Enabled = true;
        }

        private void StartConversionLinkLabel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            string backupProjectFileName = this.projectFileInfo.Name;

            if (this.backupProjectFileCheckBox.Checked)
            {
                backupProjectFileName = Regex.Replace(backupProjectFileName, ".csproj", ".Backup.csproj");
                backupProjectFileName = Regex.Replace(backupProjectFileName, ".vbproj", ".Backup.vbproj");
                backupProjectFileName = Path.Combine(this.projectFileInfo.Directory.FullName, backupProjectFileName);
                File.Copy(this.projectFileInfo.FullName, backupProjectFileName); 
            }

            this.ConvertProjectFile();
        }

        private void ConvertProjectFile()
        {
            XElement converted = new VisualStudioProjectConverter().GetConverted(this.visualStudioProject);

            string convertedProjectFileName = this.projectFileInfo.Name;

            if (!this.overwriteProjectFileCheckBox.Checked)
            {
                convertedProjectFileName = Regex.Replace(convertedProjectFileName, ".csproj", ".Silverlight.csproj");
                convertedProjectFileName = Regex.Replace(convertedProjectFileName, ".vbproj", ".Silverlight.vbproj");

                convertedProjectFileName = Path.Combine(this.projectFileInfo.Directory.FullName, convertedProjectFileName);

                if (File.Exists(convertedProjectFileName))
                {
                    File.Delete(convertedProjectFileName);
                }
            }

            converted.Save(convertedProjectFileName);

            this.convertedLabel.Visible = true;

            this.startConversionLinkLabel.Enabled = false;
        }

        private void ErrorOccuredToolStripStatusLabel_Click(object sender, EventArgs e)
        {
            MessageBox.Show(string.Format("Error message: \n{0}", this.lastError.Message), "Last error occured",  MessageBoxButtons.OK, MessageBoxIcon.Information);
            
            this.errorOccuredToolStripStatusLabel.Visible = false;
        }
    }
}